<?php
/**
 * 文章管理
 *
 * PHP Version 5.6
 *
 * @author    guanxu <510104561@qq.com>
 * @time      2017/08/11
 */
namespace Admin\Controller;
use Think\Controller;
use Common\Common\ConstantUtil;
use Think\Page;
use Common\Common\UploadUtil;
Class ArticleController extends CommonController {

    public function index() {
        $column_id = I("get.id") ? :1;;
        session("column_id",$column_id);
        $list = M("wzjs_develop_article")
            ->where(["column_id"=>$column_id])->order("id desc")->select();
        $this->assign([
            "list" => $list ,
            "column_id" => $column_id ,
        ]);
        $this->display("article/index");
    }

    public function recommend() {

        $id = I("id");
        if(empty($id)) $this->error("参数错误!" );
        M("wzjs_develop_article")->where(["id"=>$id])->save(["recommend"=>1,"recommend_time"=>date("Y-m-d H:i:s")]);
        $this->success("推荐成功!");
    }

    /**
     * 添加显示页
     */
    public function addShow() {
        $column_id = I("column_id");
        $class = M("wzjs_develop_article")->select();
        $this->assign([
            "column_id" => $column_id,
            "date" => date("Y-m-d H:i:s") ,
            "class_list" => $class
        ]);
        $this->display("article/add");
    }

    /**
     * 文章添加
     */
    public function add() {

        $data["name"]           = I("post.name");
        $data["content"]        = I("post.editorValue");
        $data["column_id"]      = I("post.parent_id");
        $data["url"]            = I("post.url");
        $data["create_user"]    = session("admin_user")["username"];
        $data["create_time"]    = date("Y-m-d H:i:s");
        $data["alt"]            = I("post.alt");
        $data["remark"]            = I("post.remark");
        $code = A("Column")->parentCode($data["column_id"]);
        $uploadImg = A("Upload")->image("","","",$code);
        if(!empty($uploadImg["title_img"])) {
            $data["img"] = "/".UploadUtil::UPLOAD_PATH."/".$code."".$uploadImg["title_img"]["savepath"].$uploadImg["title_img"]["savename"];
        }
        $result = M("wzjs_develop_article")->add($data);
        if(!empty($result))
            $this->success("添加成功!",U('Article/index?id='.$data["column_id"].''));
        else
            $this->error("添加失败!");

    }

    /**
     * 文章修改
     * @return string
     */
    public function editorShow() {
        $id = I("id");
        if(empty($id)) return "";
        $list = M("wzjs_develop_article")->where(["id"=>$id])->find();
        $this->assign([
            "list" => $list
        ]);
        $this->display('article/editor');
    }

    /**
     * 编辑
     * @return bool
     */
    public function editor() {

        $article_id = I("article_id");
        if(empty($article_id))  return false;
        $data["name"]           = I("post.name");
        $data["content"]        = I("post.editorValue");
        $data["url"]            = I("post.url");
        $data["alt"]            = I("post.alt");
        $data["remark"]         = I("post.remark");
        $data["update_user"]    = ConstantUtil::ARTICLE_USER;
        $data["update_time"]    = date("Y-m-d H:i:s");
        $column_id = M("wzjs_develop_article")->where(["id"=>$article_id])->getField("column_id");
        $code = A("Column")->parentCode($column_id);
        $uploadImg = A("Upload")->image("","","",$code);
        if(!empty($uploadImg["title_img"])) {
            $data["img"] = "/" . UploadUtil::UPLOAD_PATH . "/" . $code . "" . $uploadImg["title_img"]["savepath"] . $uploadImg["title_img"]["savename"];
        }
        M("wzjs_develop_article")->where(["id"=>$article_id])->save($data);
        $this->success("修改成功!",U('Article/index?id='.session("column_id").''));
    }


    /**
     * 修改文章状态
     */
    public function status() {
        $id = I("id");
        $article = M("wzjs_develop_article")->where(["id"=>$id])->find();
        $newStatus = ConstantUtil::changeStatus($article["status"]);
        $result = M("wzjs_develop_article")->where(["id"=>$id])->save(["status"=>$newStatus]);
        if($result == 1 )
            $this->success("修改成功",U("Article/index?id=".$article["column_id"].""));
        else
            $this->error("修改失败!");
    }

    public function del() {
        $id = I("id");
        if(empty($id)) return '';
        M("wzjs_develop_article")->where(["id"=>$id])->delete();
        $this->success("删除成功!",U("Article/index?id=".session("column_id").""));
    }

}